package com.nuaa.service.overseas.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.nuaa.dao.overseas.inter.BackSupDaoInter;
import com.nuaa.dao.overseas.inter.BackupDaoInter;
import com.nuaa.domain.overseas.BackSupEntity;
import com.nuaa.domain.overseas.BackupEntity;
import com.nuaa.domain.ret.ReturnEntity;
import com.nuaa.domain.transfer.TransSupEntity;
import com.nuaa.domain.transfer.TransferEntity;
import com.nuaa.formmodel.BackHandForm;
import com.nuaa.formmodel.RetHandForm;
import com.nuaa.service.overseas.inter.BackupServiceInter;
import com.nuaa.utils.AjaxMsg;
import com.nuaa.utils.BusinessException;
import com.nuaa.utils.CustomCodeUtil;
import com.nuaa.utils.DataGrid;
import com.nuaa.utils.PageInfo;
/***
 * 备货发货订单
 * @author heaven
 *
 */
@Service("backupService")
public class BackupServiceImpl implements BackupServiceInter{
	@Resource(name="backupDao")
	private BackupDaoInter backupDao;
	@Resource(name="backSupDao")
	private BackSupDaoInter backSupDao;
	@Transactional
	@Override
	public AjaxMsg subOneOrder(BackupEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		try{
			String BA_NO = CustomCodeUtil.getBackupOrderNo(backupDao.getMaxId(entity.getClass()));
			entity.setOrder_no(BA_NO);
			entity.setCreate_tm(new Date());
			entity.setBusiness_type("海外仓备货发货订单");
			this.backupDao.save(entity);
			msg.setSuccess(true);
			msg.setResult(BA_NO);
			msg.setMsg("提交订单成功！");
		}catch(Exception e){
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("提交失败！");
		}
		return msg;
	}

	@Override
	public DataGrid<Map<String, Object>> getAll(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from backup_order where 1=1 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=backupDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=backupDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public DataGrid<Map<String, Object>> getAllWaitBack(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from backup_order where state=0 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=backupDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=backupDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	
	@Override
	public DataGrid<Map<String, Object>> getAllProceBack(PageInfo pager, String customer_no, String orderNo,
			String tmFrom, String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from backup_order where state>0 and state<4 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=backupDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=backupDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}

	@Override
	public DataGrid<Map<String, Object>> getAllFinishBack(PageInfo pager,
			String customer_no,String orderNo,String tmFrom,String tmTo) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from backup_order where state=4 ");
		if(customer_no!=null && !"".equals(customer_no)){
			sql.append(" and customer_no ='");
			sql.append(customer_no);
			sql.append("'");
		}
		if(orderNo!=null && !"".equals(orderNo)){
			sql.append(" and order_no ='");
			sql.append(orderNo);
			sql.append("'");
		}
		if(tmFrom!=null && !"".equals(tmFrom) && tmTo!=null && !"".equals(tmTo)){
			sql.append(" and create_tm >='");
			sql.append(tmFrom);
			sql.append("' and create_tm <= '");
			sql.append(tmTo);
			sql.append("'");
		}
		try{
			long total=backupDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=backupDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Override
	public BackupEntity getById(int id) {
		return backupDao.find(BackupEntity.class, id);
	}

	@Override
	@Transactional
	public void update(BackupEntity backupOrder) {
		backupDao.update(backupOrder);
		
	}

	@Override
	public DataGrid<Map<String, Object>> getAllUncheck(PageInfo pager) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from backup_order where 1=1 and state = 0 ");
		try{
			long total=backupDao .count(sql.toString());
			int []fm=pager.getFirstindexAndMaxindex();
			List<Map<String,Object>> list=backupDao.searchForMap(sql.toString(),fm[0],fm[1]," create_tm desc ");
			return new DataGrid<Map<String, Object>>(total, list);
		}catch(Exception e){
			throw new BusinessException("获取订单信息出错！",e);
		}
	}
	@Transactional
	@Override
	public AjaxMsg subSup(BackSupEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		try{
			//首先判断是否提交过该订单的补充文档
			StringBuilder sql=new StringBuilder();
			sql.append("select * from back_sup where order_no = '");
			sql.append(entity.getOrder_no());
			sql.append("'");
			List<BackSupEntity> list = backSupDao.search(BackSupEntity.class, sql.toString());
			if(list==null || list.size()==0){
				backSupDao.save(entity);
			}else{
				/***
				 * 此处既然是覆盖就该删掉原有的文档，留做以后修正吧
				 */
				BackSupEntity temp = list.get(0);
				if(!entity.getBack_logis_tb().equals("")){
					backSupDao.delFileByName(temp.getBack_logis_tb());
					temp.setBack_logis_tb(entity.getBack_logis_tb());
				}
				if(!entity.getBack_num_detail_tb().equals("")){
					backSupDao.delFileByName(temp.getBack_num_detail_tb());
					temp.setBack_num_detail_tb(entity.getBack_num_detail_tb());
				}
				if(!entity.getOther_file().equals("")){
					backSupDao.delFileByName(temp.getOther_file());
					temp.setOther_file(entity.getOther_file());
				}
				if(!entity.getOther_file2().equals("")){
					backSupDao.delFileByName(temp.getOther_file2());
					temp.setOther_file2(entity.getOther_file2());
				}
				if(!entity.getOther_file3().equals("")){
					backSupDao.delFileByName(temp.getOther_file3());
					temp.setOther_file3(entity.getOther_file3());
				}
				if(!entity.getOther_file4().equals("")){
					backSupDao.delFileByName(temp.getOther_file4());
					temp.setOther_file4(entity.getOther_file4());
				}
				backSupDao.update(temp);
			}
			msg.setSuccess(true);
			msg.setMsg("提交成功！");
		}catch(Exception e){
			e.printStackTrace();
			msg.setSuccess(false);
			msg.setMsg("提交失败！");
		}
		return msg;
	}

	@Override
	public BackupEntity getByOrderNo(String order_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from backup_order where order_no = '");
		sql.append(order_no);
		sql.append("'");
		List<BackupEntity> list = backupDao.search(BackupEntity.class, sql.toString());
		return list.get(0);
	}

	@Override
	public BackSupEntity getSupByOrderNo(String order_no) {
		StringBuilder sql=new StringBuilder();
		sql.append("select * from back_sup where order_no = '");
		sql.append(order_no);
		sql.append("'");
		List<BackSupEntity> list = backSupDao.search(BackSupEntity.class, sql.toString());
		if(list.size()==0)
			return null;
		return list.get(0);
	}
	@Transactional
	@Override
	public AjaxMsg saveModOrder(BackupEntity entity) {
		AjaxMsg msg = new AjaxMsg();
		BackupEntity backEntity = backupDao.find(BackupEntity.class, entity.getId());
		backEntity.setAve_weight(entity.getAve_weight());
		backEntity.setType_num(entity.getType_num());
		backEntity.setLength(entity.getLength());
		backEntity.setWidth(entity.getWidth());
		backEntity.setHeight(entity.getHeight());
		backEntity.setBox_total(entity.getBox_total());
		backEntity.setTotal_num(entity.getTotal_num());
		backEntity.setTotal_orders(entity.getTotal_orders());
		try{
			backupDao.save(backEntity);
			msg.setSuccess(true);
			msg.setMsg("修改成功！");
		}catch (Exception e) {
			msg.setSuccess(false);
			msg.setMsg("修改失败！");
		}
		return msg;
	}
	@Transactional
	@Override
	public AjaxMsg saveHand(String id, BackHandForm form) {
		AjaxMsg msg = new AjaxMsg();
		try{
				BackupEntity entity = backupDao.find(BackupEntity.class,Integer.parseInt(id));
				entity.setState(form.getState());
				
				if(form.getBack_model_name()!=null&&!form.getBack_model_name().equals("")){
					backSupDao.delFileByName(entity.getBack_model_name());
					entity.setBack_model_name(form.getBack_model_name());
				}
				if(form.getRec_tab_name()!=null&&!form.getRec_tab_name().equals("")){
					backSupDao.delFileByName(entity.getRec_tab_name());
					entity.setRec_tab_name(form.getRec_tab_name());
				}
				if(form.getOther_file1()!=null&&!form.getOther_file1().equals("")){
					backSupDao.delFileByName(entity.getOther_file1());
					entity.setOther_file1(form.getOther_file1());
				}
				if(form.getOther_file2()!=null&&!form.getOther_file2().equals("")){
					backSupDao.delFileByName(entity.getOther_file2());
					entity.setOther_file2(form.getOther_file2());
				}
				if(form.getOther_file3()!=null&&!form.getOther_file3().equals("")){
					backSupDao.delFileByName(entity.getOther_file3());
					entity.setOther_file3(form.getOther_file3());
				}
				if(form.getOther_file4()!=null&&!form.getOther_file4().equals("")){
					backSupDao.delFileByName(entity.getOther_file4());
					entity.setOther_file4(form.getOther_file4());
				}
				backupDao.update(entity);
			msg.setSuccess(true);
			msg.setMsg("操作成功！");
		}catch (Exception e) {
			msg.setSuccess(false);
			msg.setMsg("操作失败！");
		}
		return msg;
	}
}
